word search
word search
link
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
For example,
Given board =
[
[‘A’,’B’,’C’,’E’],
[‘S’,’F’,’C’,’S’],
[‘A’,’D’,’E’,’E’]
]
word = “ABCCED”, -> returns true,
word = “SEE”, -> returns true,
word = “ABCB”, -> returns false.
题目大意:
给定一个二维字符数组,找出数组中是否存在某个单词,移动方向为上下左右。
解法:
显然这是一个深度优先搜索加回溯的算法
1 | class Solution { |
参考文章:
http://blog.csdn.net/worldwindjp/article/details/18041225
http://www.2cto.com/kf/201412/359314.html